home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / cipscat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  4.7 KB  |  173 lines

  1.  
  2.        /*********************************************
  3.        *
  4.        *   file d:\cips\cipscat.c
  5.        *
  6.        *   Functions: This file contains
  7.        *     main
  8.        *
  9.        *   Purpose:
  10.        *     This file contains a program that 
  11.        *     concatenates .c files together, but
  12.        *     only copies the first occurence of the
  13.        *     include cips.h statement.
  14.        *
  15.        *     This is very helpful when you need to
  16.        *     concatenate CIPS code files together
  17.        *     for compiling.
  18.        *
  19.        *     For example, the cips3.c file contains
  20.        *     the files addsub.c cutp.c and rotate.c
  21.        *     To combine these for compiling you run
  22.        *
  23.        *     cipscat addsub.c cutp.c rotate.c -o cips3.c
  24.        *
  25.        *   External Calls:
  26.        *      none
  27.        *
  28.        *   Modifications:
  29.        *      12 May 1993 - created
  30.        *
  31.        ************************************************/
  32.  
  33.  
  34. #include <stdio.h>
  35. #define  LENGTH 100
  36.  
  37. main(int argc, char *argv[])
  38. {
  39.    char input_file_name[LENGTH],
  40.         output_file_name[LENGTH],
  41.         string[LENGTH],
  42.         string2[LENGTH];
  43.  
  44.    FILE *input_file;
  45.    FILE *output_file;
  46.  
  47.    int  i, j=0;
  48.  
  49.       /*****************************************
  50.       *
  51.       *   Look at the command line to ensure
  52.       *   it is correct.
  53.       *
  54.       *****************************************/
  55.  
  56.    if(argc < 4){
  57.       printf("\nusage: cipscat in1 in2 ... inx "
  58.              "-o out");
  59.       exit(1);
  60.    }
  61.  
  62.    if(strcmp("-o", argv[argc-2]) != 0){
  63.       printf("\nusage: cipscat in1 in2 ... inx "
  64.              "-o out");
  65.       exit(1);
  66.    }
  67.  
  68.       /*****************************************
  69.       *
  70.       *   Ensure none of the input file names
  71.       *   match the output file name.
  72.       *
  73.       *****************************************/
  74.  
  75.    for(i=1; i<(argc-2); i++){
  76.       if(strcmp(argv[i], argv[argc-1]) == 0){
  77.          printf("\nERROR: input file %s has the "
  78.                 "same name as output file %s\n",
  79.                 argv[i], argv[argc-1]);
  80.          exit(2);
  81.       }
  82.    }
  83.  
  84.       /*****************************************
  85.       *
  86.       *   Open the output file.
  87.       *   Put a header comment in it.
  88.       *
  89.       *****************************************/
  90.  
  91.    strcpy(output_file_name, argv[argc-1]);
  92.    if((output_file = fopen(output_file_name, "wt")) 
  93.                              == NULL){
  94.       printf("\ncipscat: Error file %s\n", 
  95.               output_file_name);
  96.       exit(1);
  97.    }
  98.  
  99.    fputc('\n', output_file);
  100.    sprintf(string, 
  101.            "   /*************************** \n");
  102.    fputs(string, output_file);
  103.    sprintf(string, 
  104.            "   * \n");
  105.    fputs(string, output_file);
  106.    sprintf(string, "   *   %s \n", 
  107.            output_file_name);
  108.    fputs(string, output_file);
  109.    sprintf(string, 
  110.       "   *   COMPOSITE FILE COMPRISING: \n");
  111.    fputs(string, output_file);
  112.  
  113.    for(i=1; i<(argc-2); i++){
  114.       sprintf(string, "   *   %s \n", 
  115.               argv[i]);
  116.       fputs(string, output_file);
  117.    }
  118.    sprintf(string, 
  119.            "   * \n");
  120.    fputs(string, output_file);
  121.    sprintf(string, 
  122.            "   ***************************\\ \n");
  123.    fputs(string, output_file);
  124.    fputc('\n', output_file);
  125.    fputc('\n', output_file);
  126.  
  127.  
  128.       /*****************************************
  129.       *
  130.       *   Loop through the input files.
  131.       *   Copy all of the first one to the 
  132.       *   output.  For the rest, look for the
  133.       *   include cips.h statement and do not copy 
  134.       *   that to the output.
  135.       *
  136.       *****************************************/
  137.  
  138.    for(i=1; i<(argc-2); i++){
  139.  
  140.       strcpy(input_file_name, argv[i]);
  141.       if((input_file = fopen(input_file_name, "rt")) 
  142.                                 == NULL){
  143.          printf("\ncipscat: Error file %s\n", 
  144.                  input_file_name);
  145.          exit(2);
  146.       }  /* ends if fopen input_file */
  147.  
  148.       printf("\n\tcopying %s to %s",
  149.        input_file_name, output_file_name);
  150.  
  151.       if(i==1){ /* First file, so copy it all */
  152.          while(fgets(string, LENGTH, input_file))
  153.             fputs(string, output_file);
  154.       }  /* ends if first input file */
  155.  
  156.       else{ /* Other files, look for include */
  157.          while(fgets(string, LENGTH, input_file)){
  158.             if(strncmp(
  159.              string, "#include \"cips.h\"", 15) == 0)
  160.                j++;
  161.             else
  162.                fputs(string, output_file);
  163.          }
  164.       }  /* ends else all the other input files */
  165.  
  166.       fclose(input_file);
  167.    }  /* ends the main loop over i */
  168.  
  169.    fclose(output_file);
  170.    printf("\n");
  171.    return(0);
  172. }  /* ends main */
  173.